package com.supreme.securityserver.controller;

import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonentity.mysql.primary.SysUser;
import com.supreme.commonmybatis.service.quick.UserOrmService;
import com.supreme.securityserver.entity.LoginBody;
import com.supreme.securityserver.service.AuthService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "auth")
public class AuthController {

    @Resource
    private AuthService authService;

    @Resource
    private UserOrmService userOrmService;

    @GetMapping(value = "info/{userId}")
    public AjaxResp userInfo(@PathVariable(value = "userId") Long userId) {
        SysUser userInfo = userOrmService.getById(userId);
        return AjaxResp.instance(userInfo);
    }

    @PostMapping(value = "register")
    public AjaxResp register(@RequestBody LoginBody bodyData) {
        String username = bodyData.getUsername();
        String password = bodyData.getPassword();
        SysUser sysUser = authService.userRegister(username, password);
        return AjaxResp.instance(sysUser);
    }

    @PostMapping(value = "login")
    public AjaxResp login(@RequestBody LoginBody bodyData) {
        String username = bodyData.getUsername();
        String password = bodyData.getPassword();
        String token = authService.userLogin(username, password);
        return AjaxResp.instance(token);
    }

    @PostMapping(value = "logout")
    public AjaxResp logout() {
        return AjaxResp.instance();
    }
}
